<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>B页面</title>
  </head>
  <body>
    <h2 id="content">这是B页面</h2>
    <script>
      const fromOrigin = "http://localhost:8080";
      const content = document.getElementById("content");

      function receiveMessage(event) {
        console.log("B页面", event);
        if (event.origin !== fromOrigin) {
          return;
        }
        content.innerText = "这是B页面" + event.data;
        event.source.postMessage("B返回的信息", event.origin);
      }

      window.addEventListener("message", receiveMessage, false);
    </script>
  </body>
</html>
